package app.vendorportal.util;

import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.document.AbstractJExcelView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class ViewSimpleExcel extends AbstractJExcelView {
    private final Logger logger = LoggerFactory.getLogger(ViewSimpleExcel.class);

    @SuppressWarnings("unchecked")
    @Override
    protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            response.setHeader("content-disposition", "attachment;filename=" + model.get("fileName") + ".xls");
            WritableSheet ws = workbook.createSheet(model.get("mainSheetName").toString(), 0);
            WritableCellFormat wcf = new WritableCellFormat();
            wcf.setAlignment(Alignment.LEFT);
            List<String> itemTitle = (List<String>) model.get("itemTitle");
            constructTableTitle(ws, itemTitle, wcf);
            List<Object> itemList = (List<Object>) model.get("itemList");
            constructItemSheetContent(ws, itemList);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    private void constructItemSheetContent(WritableSheet ws, List<Object> itemList) throws RowsExceededException, WriteException {
        WritableCellFormat wcf = new WritableCellFormat();
        wcf.setAlignment(Alignment.LEFT);
        for (int i = 0; i < itemList.size(); i++) {
            List<Object> listtemp = (List<Object>) itemList.get(i);
            for (int j = 0; j < listtemp.size(); j++) {
                WritableCell writableCell = constructTableContent(i + 1, j, listtemp.get(j), wcf);
                if (null != writableCell) {
                    ws.addCell(writableCell);
                }
            }
        }
    }

    private WritableCell constructTableContent(int end, int j, Object object, WritableCellFormat wcf) throws WriteException, RowsExceededException {
        if (null == object) {
            return null;
        }
        if (object instanceof Integer || (Pattern.matches("\\d+", object.toString()) && object.toString().length() < 10)) {
            return new Number(j, end, Integer.parseInt(object.toString()), wcf);
        }
        return new Label(j, end, String.valueOf(object), wcf);
    }

    private void constructTableTitle(WritableSheet ws, List<String> itemTitle, WritableCellFormat wcf) throws WriteException, RowsExceededException {
        for (int i = 0; i < itemTitle.size(); i++) {
            Label labelData = new Label(i, 0, String.valueOf(itemTitle.get(i)), wcf);
            ws.addCell(labelData);
            ws.setColumnView(i, 16);
        }
    }
}